﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;

namespace Derrick.AttackTest
{
    public sealed class SharedStorage
    {
        private static volatile SharedStorage _instance;
        private static object _syncRoot = new Object();

        private static schemaAttack.DebugInfoDataTable _debugInfo;
        private static schemaAttack.DevicesForCapturingDataTable _devices;
        private static string _phonyIp;
        private static string _destinationIp;
        private static ushort _sourcePort;
        private static ushort _destinationPort;

        private SharedStorage()
        {
            _debugInfo = new schemaAttack.DebugInfoDataTable();
            _devices = new schemaAttack.DevicesForCapturingDataTable();
        }

        public schemaAttack.DebugInfoDataTable DebugInfo
        {
            get { return _debugInfo; }
        }

        public schemaAttack.DevicesForCapturingDataTable DevicesForCapturing
        {
            get { return _devices; }
        }

        public string PhonyIpAddress
        {
            get { return _phonyIp; }
            set { _phonyIp = value; }
        }

        public string DestinationIpAddress
        {
            get { return _destinationIp; }
            set { _destinationIp = value; }
        }

        public ushort SourcePort
        {
            get { return _sourcePort; }
            set { _sourcePort = value; }
        }

        public ushort DestinationPort
        {
            get { return _destinationPort; }
            set { _destinationPort = value; }
        }

        public static SharedStorage Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new SharedStorage();
                        }
                    }
                }

                return _instance;
            }
        }

        public void AddDebugInfo(string category, string description, string sourceIP, string destinationIP)
        {
            schemaAttack.DebugInfoRow debugInfoRow = _debugInfo.NewDebugInfoRow();
            debugInfoRow.Category = category;
            debugInfoRow.Description = description;
            debugInfoRow.Header_Source_IP = sourceIP;
            debugInfoRow.Header_Destination_IP = destinationIP;
            debugInfoRow.Last_User = WindowsIdentity.GetCurrent().Name;
            debugInfoRow.Last_Updated = DateTime.Now;
            _debugInfo.AddDebugInfoRow(debugInfoRow);

        }

        public void AddDeviceForCapturing(int index, string description)
        {
            schemaAttack.DevicesForCapturingRow deviceRow = _devices.NewDevicesForCapturingRow();
            deviceRow.Device_ID = index;
            deviceRow.Description = description;
            _devices.AddDevicesForCapturingRow(deviceRow);
        }

    }
}
